// src/utils/entityOperations.ts
import { ElMessageBox, ElNotification } from 'element-plus';
import { getRelationByFrom, deleteRelation } from '/@/api/thingsborad/entity/entity';
import { relationByFromParams } from '/@/api/thingsborad/entity/model/entityModel';

export const useEntityOperations = () => {
  // 删除菜单项
  const deleteMenuItem = async (
    itemId: string,
    parentId: string,
    fourthLayerData: Record<string, any[]>,
    loadDataFn: (item: any) => Promise<void>
  ) => {
    try {
      await ElMessageBox.confirm('确定要删除此项吗？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      });

      // 调用删除接口
      await deleteRelation(itemId);

      // 刷新本地数据
      if (fourthLayerData[parentId]) {
        fourthLayerData[parentId] = fourthLayerData[parentId].filter(
          item => item.to.id !== itemId
        );
      }

      // 重新加载父级数据
      await loadDataFn({ to: { id: parentId } });

      ElNotification({
        title: '成功',
        message: '删除成功',
        type: 'success',
        duration: 2000
      });
    } catch (error) {
      if (error !== 'cancel') {
        ElNotification({
          title: '错误',
          message: `删除失败: ${error.message}`,
          type: 'error',
          duration: 3000
        });
      }
    }
  };

  return { deleteMenuItem };
};

